﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Net.Mail;
using System.Net;
using System.Resources;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    Operation operation = new Operation();　//声明业务类
    Random random = new Random();　　　　　//声明随机类对象
    protected void Page_Load(object sender, EventArgs e)
    {
        msg.Visible = false;
        lbName.Visible = false;
        if (!IsPostBack)
        {
            lblCheckCode.Text = random.Next(1000, 9999).ToString();
        }
    }

    protected void BT1_Click(object sender, EventArgs e)
    {
        if (RB1.Checked)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            this.type.Attributes.Add("onchange", "typeChange(this)");
            ClientScript.RegisterStartupScript(Page.GetType(), "", 
                "<script>typeChange(document.getElementById('" + type.ClientID + "'));</script>");
        }
    }
    protected void BT2_Click(object sender, EventArgs e)
    {
        string Type = type.SelectedValue;
        string Name = name.Text.ToString();
        string Pwd = password.Text.ToString();
        
        string Tel = tel.Text.ToString();
        string Email = email.Text.ToString();
        string QQ = qq.Text.ToString();
        string MSN = msn.Text.ToString();
        string Comname = tbComname.Text.Trim();
        String Address = tbAddress.Text.Trim();
        string Info = tbOrgDetail.Text.Trim();
        if (isValidate(Email, Name))
        {
            try
            {
                if (Type.Equals("专业人士")) Comname = "";
                operation.InsertUser(Name, Pwd, Type, Email, Tel, QQ, MSN,Comname,Address,Info);
                DataSet ds = operation.ValidateEmail(Email);
                string uid = ds.Tables["tb_user"].Rows[0][0].ToString();
                sendActiveMail(Email, Name, uid);
            }
            catch (Exception ex) { }
            
        }
    }
    public bool isValidate(String Email, string Name)
    {
        int count;
        bool result = true;
        try
        {
            count = operation.ValidateEmail(Email).Tables["tb_user"].Rows.Count;  //判断用户是否合法。
            int c2 = operation.ValidateUser(Name).Tables["tb_user"].Rows.Count;
            if (count > 0)
            {
                msg.Visible = true;
                msg.Text = "邮箱已经注册，如果未激活请激活";
                email.Text = "";
                email.Focus();
                result = false;
            }
            else
            {
                msg.Visible = false;
            }
            if (c2 > 0)
            {
                lbName.Visible = true;
                lbName.Text = "用户名已经存在，如果未激活请激活";
                name.Text = "";
                name.Focus();
                result = false;
            }
            else
            {
                lbName.Visible = false;
            }
        }
        catch (Exception e)
        {
            //Response.Write(e);
        }
        //RegisterStartupScript("", "<script language=javascript>alert('用户名存在');</script>");
        return result;
    }
    public void sendActiveMail(string toEmailstring, string DisplayName, string userId)
    {
        string FROM = "dmdm2802@gmail.com";
        string SUBJECT = "激活新用户";
        string BODY = File.ReadAllText(Server.MapPath("NewActiveEmail.txt"));
        BODY = BODY.Replace("[Name]", DisplayName);
        String svrname = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
        String port = ":" + HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
        BODY = BODY.Replace("[LINK]", "http://" + svrname + port + "/UserRegister/Activate.aspx?UserID=" +
            userId + "&Em=" + Server.UrlEncode(toEmailstring.Trim()));
        BODY = BODY.Replace("[UserName]", DisplayName.Trim());
        BODY = BODY.Replace("[Pwd]", "******");
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(FROM, "科研成果超市");
        msg.To.Add(toEmailstring);
        msg.Subject = SUBJECT;
        msg.Body = BODY;
        msg.BodyEncoding = System.Text.Encoding.UTF8;
        msg.IsBodyHtml = false;
        msg.ReplyTo = new MailAddress(FROM);
        //smtp.126.com不可用，所有使用smtp.gmail.com
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.Credentials = new NetworkCredential("dmdm2802", "<pwd>2010");
        smtp.EnableSsl = true;
        try
        {
            smtp.Send(msg);
            Response.Redirect("save.html");
        }
        catch (Exception e)
        {
            Response.Write("<script language='javascript' type='text/javascript'>alert('系统繁忙，你的激活邮件可能没有发出，请联系管理员！谢谢');</script>");
        }
    }
}
